import asyncio
import importlib
import time
import os
import shutil
from os.path import getctime, join

from models.task import task_model
from models.ziniao_accounts import ziniao_accounts_model
from utils.ziniao import ZiNiaoBrowserTools
from utils.async_pool import AsyncPool
from database.redis_clients import RedisClients
from config import settings
from utils.log import logger


async def remove_cache():
    try:
        res = await RedisClients.async_cli.set("remove_ziniao_cache", 1, nx=True, ex=86400)
        if res:
            cache_path = settings.ziniao.cache_path
            for dirname in os.listdir(settings.ziniao.cache_path):
                if time.time() - getctime(join(cache_path, dirname)) > 86400:
                    shutil.rmtree(join(cache_path, dirname))
    except Exception:
        ...


def remove_cache_callback(fut: asyncio.tasks.Task):
    ...


def exit_ziniao():
    item = ziniao_accounts_model.sync_find_one({})
    if item:
        zi = ZiNiaoBrowserTools()
        zi.exit(item.json(include={"company", "username", "password"}))


async def main():
    try:
        item = await task_model.get()
        if item:
            module_path, cls = item.module_reference.rsplit(".", 1)
            module = importlib.import_module(module_path)
            instance = getattr(module, cls)(item)
            await instance.main()
        await asyncio.sleep(1)
    except Exception as e:
        logger.exception("爬虫运行异常~！")


def run():
    pool = AsyncPool(maxsize=settings.max_spider_running_numbers)
    while 1:
        pool.submit(main())
        pool.submit(remove_cache(), remove_cache_callback)
        time.sleep(1)


if __name__ == '__main__':
    try:
        run()
    except KeyboardInterrupt:
        exit_ziniao()
